package com.lanou.musicbox.fragment.onlinemusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.onlinemusic.EnterTheSingerMenuAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.EnterTheSongs;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.download.CommonProgressDialog;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 赵宝龙 on 15/10/22.
 * 进入歌单页面
 */


public class EnterThePlaylistFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    private CollapsingToolbarLayout collapsingToolbarLayout;
    private ListView entertheListView;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private EnterTheSongs data;
    private List<EnterTheSongs.ContentEntity> datas;
    private EnterTheSingerMenuAdapter adapter;

    private ImageView pic, EnterSongs_back;
    private TextView title, tag, listenum, number, desc;
    private String url_ALL;

    private ListView listView;
    private CommonProgressDialog commonProgressDialog;// 下载

    public EnterThePlaylistFragment() {

        super();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.enterthe_song, null);
        entertheListView = (ListView) v.findViewById(R.id.entertheListView);
        entertheListView.setCacheColorHint(1);
        pic = (ImageView) v.findViewById(R.id.pic);
        EnterSongs_back = (ImageView) v.findViewById(R.id.EnterSongs_back);
        title = (TextView) v.findViewById(R.id.title);
        tag = (TextView) v.findViewById(R.id.tag);
        listenum = (TextView) v.findViewById(R.id.listenum);
        number = (TextView) v.findViewById(R.id.number);
        desc = (TextView) v.findViewById(R.id.desc);

        listView = (ListView) v.findViewById(R.id.entertheListView);

        collapsingToolbarLayout = (CollapsingToolbarLayout) v.findViewById(R.id.collapsing_toolbar);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        collapsingToolbarLayout.setTitleEnabled(true);
        EnterSongs_back.setOnClickListener(this);
        listView.setOnItemClickListener(this);

        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        imageLoader = volleySingleton.getImageLoader();

        Bundle bundle = getArguments();
        String songs_id = bundle.getString("songs_id");

        url_ALL = Contant.Url.URL_Enter_PlayList + songs_id;
        Log.i("lanou333333", "on_url_ALL " + url_ALL);

        initData();

    }

    private void initData() {
        StringRequest request = new StringRequest(url_ALL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                data = gson.fromJson(response, EnterTheSongs.class);
                datas = data.getContent();
                Log.e("ZBL", "!!!!!!!!!!!!!!!!!!!!!" + response);
                adapter = new EnterTheSingerMenuAdapter(data, getActivity());
                initView();
                entertheListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    private void initView() {

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(pic, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(data.getPic_500(), listener);

        tag.setText(data.getTag());
        listenum.setText(data.getListenum());
        collapsingToolbarLayout.setTitle(data.getTitle());
        if (datas != null) {
            number.setText(String.valueOf(datas.size()));
        }
        desc.setText(data.getDesc());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.EnterSongs_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<Entity> songsInfos = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            Entity netSongs = new Entity();
            String song_id = datas.get(i).getSong_id();
            String title = datas.get(i).getTitle();
            String pic_small = data.getPic_300();
            String pic_premium = data.getPic_700();
            String author = datas.get(i).getAuthor();


            netSongs.setSongs_id(song_id);
            netSongs.setTitle(title);
            netSongs.setSmall_pic(pic_small);
            netSongs.setBig_pic(pic_premium);
            netSongs.setArtist(author);
            netSongs.setItem_id(position);

            songsInfos.add(i, netSongs);
        }
        String whatMusic = "网络";
        Events.MusicInfoEvent musicInfoEvent = new Events.MusicInfoEvent();
        musicInfoEvent.setMp3Infos(songsInfos, whatMusic);
        EventBus.getDefault().post(musicInfoEvent);
    }
}
